{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lesson 6 - Taking Input, Reading and Writing Files, Functions\n",
    "\n",
    "### Readings\n",
    "\n",
    "* Shaw: [Exercises 11-26](https://learnpythonthehardway.org/python3/ex11.html)\n",
    "* Lutz: Chapters 9, 14-17\n",
    "\n",
    "### Table of Contents\n",
    "\n",
    "* [Taking Input](#input)\n",
    "* [Reading Files](#reading)\n",
    "* [Writing Files](#writing)\n",
    "* [Functions](#functions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"input\"></a>\n",
    "\n",
    "### Taking Input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Shaw's _Learn Python The Hard Way_, he uses `input()` and `argv` to take input from the user. These don't work very well with Jupyter notebooks, but we will cover them because they can be useful in Python scripts."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `input()`\n",
    "\n",
    "In Python 2, this command was `raw_input()`; in Python 3, it's been renamed `input()`. Newer versions of Jupyter Notebook support this kind of input, but it's kind of weird. Better to just 'hard code' the value for a variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "x = input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `argv`\n",
    "\n",
    "When you import the `argv` special variable, it allows you to pass strings, numbers, and filenames to your python code. It doesn't work in Jupyter notebooks, however, so you'll have to use a workaround. We can comment out the `argv` calls and hard code the values we would have passed. Later, when we select \"Download as > Python (.py)\", we can open up that .py file and uncomment the `argv` calls. Still, it's a good idea to define all your variables and file paths at the start of your notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "script: something.py\n",
      "first: 5\n",
      "second: 6\n",
      "third: hello\n"
     ]
    }
   ],
   "source": [
    "#from sys import argv\n",
    "\n",
    "script = 'something.py' #argv[0]\n",
    "value1 = 5 #argv[1]\n",
    "value2 = 6 #argv[2]\n",
    "value3 = 'hello' #argv[3]\n",
    "\n",
    "print(\"script: %s\\nfirst: %s\\nsecond: %s\\nthird: %s\" % (script, value1, value2, value3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the Python script, we would uncomment the argv calls like so:\n",
    "\n",
    "```python\n",
    "from sys import argv\n",
    "\n",
    "script = argv[0]\n",
    "value1 = argv[1]\n",
    "value2 = argv[2]\n",
    "value3 = argv[3]\n",
    "\n",
    "print(\"script: %s\\nfirst: %s\\nsecond: %s\\nthird: %s\" % (\n",
    "    script, value1, value2, value3))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adding code to the begging of your code would check if the correct number of arguments is entered and exit if not:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "from sys import argv\n",
    "from sys import exit\n",
    "\n",
    "if len(argv) != 4:\n",
    "    print(\"Usage: python ex.py value1 value2 value3\")\n",
    "    exit(1)\n",
    "\n",
    "script = argv[0]\n",
    "value1 = argv[1]\n",
    "value2 = argv[2]\n",
    "value3 = argv[3]\n",
    "\n",
    "print(\"script: %s\\nfirst: %s\\nsecond: %s\\nthird: %s\" % (\n",
    "    script, value1, value2, value3))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `click`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Click is a conda- and pip-installable package that simplifies command-line interfaces and handling of arguments. Read the documentation here: http://click.pocoo.org/5/.\n",
    "\n",
    "Here is an example of a simple Click program:\n",
    "\n",
    "```python\n",
    "import click\n",
    "\n",
    "@click.command()\n",
    "@click.option('--count', default=1, help='Number of greetings.')\n",
    "@click.option('--name', prompt='Your name',\n",
    "              help='The person to greet.')\n",
    "def hello(count, name):\n",
    "    \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n",
    "    for x in range(count):\n",
    "        click.echo('Hello %s!' % name)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    hello()\n",
    "```\n",
    "\n",
    "And what it looks like when run:\n",
    "\n",
    "```\n",
    "$ python hello.py --count=3\n",
    "Your name: John\n",
    "Hello John!\n",
    "Hello John!\n",
    "Hello John!\n",
    "```\n",
    "\n",
    "It automatically generates nicely formatted help pages:\n",
    "\n",
    "```\n",
    "$ python hello.py --help\n",
    "Usage: hello.py [OPTIONS]\n",
    "\n",
    "  Simple program that greets NAME for a total of COUNT times.\n",
    "\n",
    "Options:\n",
    "  --count INTEGER  Number of greetings.\n",
    "  --name TEXT      The person to greet.\n",
    "  --help           Show this message and exit.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Beginning your notebook or script"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Without using argv or click, a typical script or IPython notebook might begin like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import required packages\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define file paths and variables\n",
    "path_input_file = '~/sio209/input.txt'\n",
    "path_output_file = '~/sio209/output.txt'\n",
    "iterations = 10\n",
    "evalue = 1e-5\n",
    "color = 'dark blue'\n",
    "title = 'My plot'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"reading\"></a>\n",
    "\n",
    "### Reading Files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can read in a text file using `open()` and then print or use it all at once or one line at a time. Note that when we read the lines of a file, the lines are removed from the file handle object (called a `TextIOWrapper`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = '../data/woodchuck.txt'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read all at once"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content of file '../data/woodchuck.txt':\n",
      "How much wood\n",
      "Would a woodchuck chuck\n",
      "If a woodchuck could chuck wood?\n",
      "\n"
     ]
    }
   ],
   "source": [
    "txt = open(filename)\n",
    "\n",
    "print(\"Content of file %r:\" % filename)\n",
    "print(txt.read())\n",
    "\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'How much wood\\nWould a woodchuck chuck\\nIf a woodchuck could chuck wood?\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt = open(filename)\n",
    "\n",
    "txt.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "_io.TextIOWrapper"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read one line at a time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'How much wood\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt = open(filename)\n",
    "\n",
    "txt.readline()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Would a woodchuck chuck\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt.readline()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'If a woodchuck could chuck wood?\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt.readline()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt.readline()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read lines as a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['How much wood\\n',\n",
       " 'Would a woodchuck chuck\\n',\n",
       " 'If a woodchuck could chuck wood?\\n']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "txt = open(filename)\n",
    "\n",
    "txt.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Open in a `with` block. Then use `for` loop, `read()`, `readline()`, or `readlines()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "How much wood\n",
      "Would a woodchuck chuck\n",
      "If a woodchuck could chuck wood?\n"
     ]
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    for line in f:\n",
    "        line = line.rstrip()\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'How much wood\\nWould a woodchuck chuck\\nIf a woodchuck could chuck wood?\\n'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    lines = f.read()\n",
    "lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'How much wood\\n'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    line = f.readline()\n",
    "line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['How much wood\\n',\n",
       " 'Would a woodchuck chuck\\n',\n",
       " 'If a woodchuck could chuck wood?\\n']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    lines = f.readlines()\n",
    "lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['How much wood',\n",
       " 'Would a woodchuck chuck',\n",
       " 'If a woodchuck could chuck wood?']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    lines = [line.rstrip() for line in f.readlines()]\n",
    "lines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Pandas can also read files, but it's better with tables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(filename, header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>How much wood</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Would a woodchuck chuck</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>If a woodchuck could chuck wood?</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  0\n",
       "0                     How much wood\n",
       "1           Would a woodchuck chuck\n",
       "2  If a woodchuck could chuck wood?"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"writing\"></a>\n",
    "\n",
    "### Writing Files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can write files using `write()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "outfile = 'limerick.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# some text to write (a limerick by Edward Lear)\n",
    "line1 = \"There was an Old Man with a beard\\nWho said, 'It is just as I feared!\"\n",
    "line2 = \"Two Owls and a Hen\\nFour Larks and a Wren,\"\n",
    "line3 = \"Have all built their nests in my beard!'\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write the most basic way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = open(outfile, 'w')\n",
    "\n",
    "target.write(line1)\n",
    "target.write('\\n')\n",
    "target.write(line2)\n",
    "target.write('\\n')\n",
    "target.write(line3)\n",
    "target.write('\\n')\n",
    "\n",
    "target.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "_io.TextIOWrapper"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write in a `with` block\n",
    "\n",
    "Again, we can use `with` to simplify things (avoid having to `close()` the file)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(outfile, 'w') as target:\n",
    "    target.write(line1)\n",
    "    target.write('\\n')\n",
    "    target.write(line2)\n",
    "    target.write('\\n')\n",
    "    target.write(line3)\n",
    "    target.write('\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Write with Pandas to comma-separated values or tab-separated values\n",
    "\n",
    "The dataframe `df` contains the woodchuck text from above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('woodchuck_pandas.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('woodchuck_pandas.tsv', sep='\\t')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"functions\"></a>\n",
    "\n",
    "### Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Functions allow you to carry out the same task multiple times. This reduces the amount of code you write, reduces mistakes, and makes your code easier to read."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Printing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def say_hello():\n",
    "    print('Hello, world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world!\n"
     ]
    }
   ],
   "source": [
    "say_hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_a_string(foo):\n",
    "    print('%s' % foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is a string.\n"
     ]
    }
   ],
   "source": [
    "print_a_string('Here is a string.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A string saved as a variable.\n"
     ]
    }
   ],
   "source": [
    "x = 'A string saved as a variable.'\n",
    "print_a_string(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300\n"
     ]
    }
   ],
   "source": [
    "y = 300\n",
    "print_a_string(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_two_things(one, two):\n",
    "    print('%s AND %s' % (one, two))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yes AND 10\n"
     ]
    }
   ],
   "source": [
    "x = 'yes'\n",
    "y = 10\n",
    "print_two_things(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_three_things(*blob):\n",
    "    v1, v2, v3 = blob\n",
    "    print('%s, %s, %s' % (v1, v2, v3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a, 31, ['x', 'y', 'z']\n"
     ]
    }
   ],
   "source": [
    "print_three_things('a', 31, ['x', 'y', 'z'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_two(num1, num2):\n",
    "    print(num1 + num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "add_two(10, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.7\n"
     ]
    }
   ],
   "source": [
    "add_two(1.3, 4.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AAAbbb\n"
     ]
    }
   ],
   "source": [
    "add_two('AAA', 'bbb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Returning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def return_sum(a, b):\n",
    "    return(a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "return_sum(5, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = return_sum(8, 13)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def combine_with_commas(*blob):\n",
    "    mystring = ''\n",
    "    for element in blob:\n",
    "        mystring = mystring + str(element) + ','\n",
    "    mystring = mystring[:-1]\n",
    "    return(mystring)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'40,50,60'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combine_with_commas(40, 50, 60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'40,50,60,70,hello'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combine_with_commas(40, 50, 60, 70, 'hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we have to redefine this function to return instead of print\n",
    "def add_two(num1, num2):\n",
    "    return(num1 + num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 100\n",
    "y = 100\n",
    "z = add_two(x, y)\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'100100'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = '100'\n",
    "b = '100'\n",
    "c = add_two(a, b)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = '100'\n",
    "b = '100'\n",
    "c = add_two(int(a), int(b))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sum_product_exponent(v1, v2):\n",
    "    s = v1 + v2\n",
    "    p = v1 * v2\n",
    "    e = v1 ** v2\n",
    "    return(s, p, e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7, 10, 32)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_product_exponent(2, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_sum, my_product, my_exponent = sum_product_exponent(2, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_exponent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
